package com.mall.controller.backend;

import com.mall.common.Const;
import com.mall.common.MySessionContext;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.Category;
import com.mall.pojo.User;
import com.mall.service.ICategoryService;
import com.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by xiaoyao on 2017/8/22.
 */
@Controller
@RequestMapping("/manage/category/")
public class categoryManagerController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICategoryService iCategoryService;
    /**
     * 添加分类节点
     * @return
     * 1、判断登录的是否是管理员；
     * 2、指定添加分类的名字和父节点，如果没有指定父节点就是根节点0的子节点；
     */
    @RequestMapping(value = "addCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addCategory(String categoryName,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //判断登录用户身份为管理员则执行添加分类方法；
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iCategoryService.addCategory(categoryName,parentId);
        }
        return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
    }

    /**
     * 修改分类名
     * @param categoryId
     * @param categoryName
     * @return
     */
    @RequestMapping(value = "setCategoryName.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setCategoryName(Integer categoryId,String categoryName){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //判断登录用户身份为管理员则执行更新分类名称的方法；
        if(iUserService.checkAdminRole(user).isSuccess()){
            return iCategoryService.updateCategoryName(categoryId,categoryName);
        }
        return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
    }


    /**
     * 获取平级的分类子节点信息（即子分类信息）
     * @param categoryId
     * @return
     */
    @RequestMapping(value="getCategory.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("登录用户信息获取异常！");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询子节点的category信息，并且不递归，保持平级；
            return iCategoryService.getChildrenParallelCategory(categoryId);
        }
        return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
    }


    /**
     * 获取所有的子分类
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "getDeepCategory.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询当前节点的id和递归子节点的id；
            return iCategoryService.getCategoryAndChildrenById(categoryId);
        }
        return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
    }







}
